package shelly.basic.class01;

import shelly.Util;

import java.util.Arrays;

/**
 * @author: Shelly
 * @create: 2023-01-13 22:28:38
 * @describe: 插入排序
 */
public class Code03_InsertionSort {
    public static void sort(int[] arr){
        if (arr.length == 0 || arr.length == 1){
            return;
        }
        int n = arr.length;
        // 左侧有序，i 记录右侧位置
        for (int i = 1;i < n;i++){
            for (int j = i - 1;j>=0;j--){// j 可以倒过来排序
                if (arr[j] > arr[j+1]){
                    Util.swap(arr,j,j+1);
                }else{
                    break;
                }
            }
        }
    }
    public static void main(String[] args) {
        int[] arr = {18,3,6,3,9,43,23,12,6,7};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
